
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using Util;

namespace API.Controllers
{
    public class Base_UserRoleController : BaseController
    {
        Repository<tBase_UserRole> _Base_UserRole;
        readonly IMapper _mapper;
        public Base_UserRoleController(Repository<tBase_UserRole> Base_UserRole, IMapper mapper)
        {
            _Base_UserRole = Base_UserRole;
            _mapper = mapper;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultMsg<string> Save([FromBody] SaveBase_UserRoleInput dto)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                var model = _mapper.Map<tBase_UserRole>(dto);
                InitEntity(model);
                _Base_UserRole.InsertOrUpdateAsync(model);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_UserRoleController--Save:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultMsg<List<Base_UserRoleOutput>> GetList([FromBody] Base_UserRoleListInput dto)
        {
            ResultMsg<List<Base_UserRoleOutput>> result = new ResultMsg<List<Base_UserRoleOutput>>();
            try
            {
                if (dto.PageNum <= 0)
                {
                    return result.Error("PageNum不能小于或等于0");
                }
                if (dto.PageSize <= 0)
                {
                    return result.Error("PageSize不能小于或等于0");
                }
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel
                {
                    PageIndex = dto.PageNum,
                    PageSize = dto.PageSize,
                };
                var exp = Expressionable.Create<tBase_UserRole>();
                //if (!string.IsNullOrWhiteSpace(dto.Name))
                //{
                //    exp.And(x => x.FName.Contains(dto.Name));
                //}
                var list = _Base_UserRole.GetPageList(exp.ToExpression(), pageModel).OrderByDescending(x => x.CreateTime).ToList();
                var Base_UserRoleOutputs = _mapper.Map<List<Base_UserRoleOutput>>(list);
                return result.Success(Base_UserRoleOutputs, pageModel.TotalCount);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_UserRoleController--GetList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 获取唯一
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultMsg<Base_UserRoleOutput> GetTheOne(string id)
        {
            ResultMsg<Base_UserRoleOutput> result = new ResultMsg<Base_UserRoleOutput>();
            try
            {
                var model = _Base_UserRole.GetById(id);
                var dao = _mapper.Map<Base_UserRoleOutput>(model);
                return result.Success(dao);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_UserRoleController--GetTheOne:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultMsg<string> Del(string id)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                _Base_UserRole.DeleteById(id);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Base_UserRoleController--Del:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
    }
}
